<!--
 * @Author: your name
 * @Date: 2022-04-21 15:17:46
 * @LastEditTime: 2022-04-21 15:26:46
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /测试文档/25.html
-->
<script src="">
   const data = [
        [120.19419188500001, 30.319325033999974],
        [120.19252369599997, 30.319325564999986],
        [120.19239380299996, 30.32097010299998],
        [120.19244600499997, 30.320973040000013],
        [120.19314340599999, 30.320960429000024],
        [120.19335329600005, 30.320929562999993],
        [120.19356367800003, 30.32092575600001],
        [120.19387554399998, 30.320920113],
        [120.19390077499997, 30.32091786799998],
        [120.193925251, 30.32091210499999],
        [120.19394832800003, 30.320902976000013],
        [120.19396939299997, 30.32089072299999],
        [120.193987891, 30.32087567100001],
        [120.19400333199997, 30.320858215999976],
        [120.19401530799996, 30.320838820000006],
        [120.19402350300004, 30.320817997999995],
        [120.19406534100006, 30.320674429999997],
        [120.194098812, 30.32044915900002],
        [120.19414978400005, 30.320274251],
        [120.19423897599995, 30.31996816100002],
        [120.194289948, 30.319793253],
        [120.194366403, 30.319530886999985],
        [120.19437051399996, 30.319510046999994],
        [120.194370833, 30.319488906000004],
        [120.19436735299996, 30.319467979000024],
        [120.19436015999997, 30.319447778999972],
        [120.19433542299998, 30.31941149300002],
        [120.19431848199997, 30.319396294],
        [120.19429901900003, 30.319383567999978],
        [120.19419188500001, 30.319325033999974]
      ];
  reader.onload = function () {
    shapefile
      .open(this.result)
      .then(source => {
        source.read().then(function log(result) {
          if (result.done) {
            that.$message({
              showClose: true,
              message: "加载成功",
              duration: 4000
            });
            return;
          }
          let addPolygon = [];
          const addPolygon_xyObject = [];
          // const addPolygon_degree = [];
          for (
            let i = 0;
            i < result.value.geometry.coordinates.length;
            i++
          ) {
            if (
              typeof result.value.geometry.coordinates[0][0][0][0] ===
              "number"
            ) {
              for (
                let j = 0;
                j < result.value.geometry.coordinates[i].length;
                j++
              ) {
                for (
                  let k = 0;
                  k < result.value.geometry.coordinates[i][j].length;
                  k++
                ) {
                  addPolygon.push(
                    chaos.cesium.Cartesian3.fromDegrees(
                      result.value.geometry.coordinates[i][j][k][0],
                      result.value.geometry.coordinates[i][j][k][1],
                      3
                    )
                  );
                  addPolygon_xyObject.push({
                    x: result.value.geometry.coordinates[i][j][k][0],
                    y: result.value.geometry.coordinates[i][j][k][1]
                  });
                  // addPolygon_degree.push([
                  //   result.value.geometry.coordinates[i][j][k][0],
                  //   result.value.geometry.coordinates[i][j][k][1]
                  // ]);
                }
              }
            } else if (
              typeof result.value.geometry.coordinates[0][0][0] === "number"
            ) {
              for (
                let j = 0;
                j < result.value.geometry.coordinates[i].length;
                j++
              ) {
                addPolygon.push(
                  chaos.cesium.Cartesian3.fromDegrees(
                    result.value.geometry.coordinates[i][j][0],
                    result.value.geometry.coordinates[i][j][1],
                    3
                  )
                );
                addPolygon_xyObject.push({
                  x: result.value.geometry.coordinates[i][j][0],
                  y: result.value.geometry.coordinates[i][j][1]
                });
                // addPolygon_degree.push([
                //   result.value.geometry.coordinates[i][j][0],
                //   result.value.geometry.coordinates[i][j][1]
                // ]);
              }
            }
            const polygonId = name + "_" + num + "_" + i;
            that.addedEntIdsList.push(polygonId);
            Viewer.entities.add({
              id: polygonId,
              polygon: {
                hierarchy: addPolygon,
                fill: true,
                material: chaos.cesium.Color.fromCssColorString(
                  "rgba(216, 255, 204, 0.4)"
                ),
                perPositionHeight: true,
                outline: true,
                outlineWidth: 1.5,
                outlineColor: chaos.cesium.Color.BLACK.withAlpha(0.6),
                heightReference:
                  chaos.cesium.HeightReference.RELATIVE_TO_GROUND
              }
            });
            //that.positionSelected = addPolygon_degree;
            that.positionSelected = [addPolygon_xyObject];
            try {
              const regionCenter = turf.center(
                turf.polygon([
                  that.transPointsObjectToArray(addPolygon_xyObject)
                ])
              );
              Viewer.camera.flyTo({
                destination: chaos.cesium.Cartesian3.fromDegrees(
                  regionCenter.geometry.coordinates[0],
                  regionCenter.geometry.coordinates[1],
                  30000
                )
              });
            } catch (e) {
              console.log(e);
            }
            addPolygon = [];
          }

          num += 1;
          //that.$refs.refFile.value = null;
          return source.read().then(log);
        });
      })
      .catch(error => console.error(error.stack));
  };
</script>